package com.scu.qingshi.domain.authorize.model;

import cn.hutool.core.lang.UUID;
import com.scu.qingshi.common.exception.authorization.AuthorizationException;
import com.scu.qingshi.domain.authorize.enums.AuthorizationStatusEnum;
import com.scu.qingshi.infrastructure.dao.po.User;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * 授信单
 * @author qingshi
 */
@Data
@Slf4j
@Deprecated
public class AuthorizationOrder {

    /**
     * 电话
     */
    private String phone;

    /**
     * 授信单状态
     * {@link com.scu.qingshi.domain.authorize.enums.AuthorizationStatusEnum}
     */
    private String status;

    /**
     * 授信单号
     */
    private String creditNo;


    private AuthorizationOrder(){};

    public static AuthorizationOrder init(){
        AuthorizationOrder authorizationOrder = new AuthorizationOrder();
        authorizationOrder.status = AuthorizationStatusEnum.INIT.name();
        authorizationOrder.creditNo = String.valueOf(UUID.randomUUID());
        return authorizationOrder;
    }

    public void register(User user) throws AuthorizationException {
        if (Objects.equals(this.status, AuthorizationStatusEnum.INIT.name())){
            this.status = AuthorizationStatusEnum.REGISTERED.name();
            user.setStatus(AuthorizationStatusEnum.REGISTERED.name());
        }else {
            log.error("错误的授权状态，不能注册");
            throw AuthorizationException.dataException("错误的授权状态，不能注册");
        }
    }

    public void cancel() throws AuthorizationException {
        if (Objects.equals(this.status, AuthorizationStatusEnum.REGISTERED.name())){
            this.status = AuthorizationStatusEnum.CANCELED.name();
        }else {
            log.error("错误的授权状态，不能注销");
            throw AuthorizationException.dataException("错误的授权状态，不能注销");
        }
    }

}
